<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<block th:replace="commonBase"></block>   
</head>
<body class="hold-transition skin-blue layout-top-nav">
	<div class="mini-splitter" handlerSize="0.1" style="width:100%;height:100%;">
	    <!-- 左部查询树开始 -->
		<div size="20%" showCollapseButton="false">
			<div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;">
				<span style="padding-left:5px;">名称：</span> <input id="key"
					emptyText="请输入机构名称" class="mini-textbox" style="width:120px;"
					onenter="orgDimTreeSearch" AUTOCOMPLETE="on" /> <a
					class="mini-button" iconCls="icon-search" plain="true"
					onclick="orgDimTreeSearch()">查找</a> 
			</div>
			<div class="mini-fit">
				<ul id="tree1" class="mini-tree" 
					style="width:100%;" showTreeIcon="true" textField="orgName"
					idField="id" expandOnLoad="0" onnodeclick="csearch()"
					parentField="parentId" resultAsTree="false" allowDrag="true"
					allowDrop="true">
				</ul>
			</div>
		</div>
	    <!-- 左部查询树结束 -->
	    
	    <!-- 右部查询列表开始 -->
		<div showCollapseButton="false">
		    <!-- 菜单栏开始 -->
			<div style="width:100%;">
				<div class="mini-toolbar" style="border-bottom:0;padding:0px;">
					<table style="width:100%;">
						<tr>
							<td style="width:100%;" id="operationId">
							  <a sec:authorize="hasAuthority('user:add')" class="mini-button" plain="true" iconCls="icon-addnew" onclick="add">添加</a>
							  <a sec:authorize="hasAuthority('user:update')" class="mini-button" plain="true" iconCls="icon-edit" onclick="edit">编辑</a>
							  <a sec:authorize="hasAuthority('user:delete')" class="mini-button" plain="true" iconCls="icon-remove" onclick="deleteUser">删除</a>
							  <a sec:authorize="hasAuthority('user:toRole')" class="mini-button" plain="true" iconCls="icon-user" onclick="toRole">角色分配</a>
							</td>
							<td style="white-space:nowrap;"><input id="searchKey"
								class="mini-textbox" emptyText="请输入名称或编码" style="width:150px;"
								onenter="search" /> <a class="mini-button" onclick="search()" iconCls="icon-search">查询</a>
							</td>
						</tr>
					</table>
				</div> 
			</div>
			<!-- 菜单栏结束 -->
			<!-- 列表展示开始 -->
			<div class="mini-fit">
				<div id="grid1" class="mini-datagrid" style="width:100%;height:100%;"   
					allowCellSelect="true" multiSelect="true" allowAlternating="false"
					sortField="USER_CODE" sortOrder="asc" idField="id" pageSize="20"
					sizeList="[10,20,50,100]" th:attr="url=@{/user/userList}">
					<div property="columns">
						<div type="checkcolumn"></div>
						<div field="userCode" sortField="USER_CODE" width="100" allowSort="true" headerAlign="center" align="left">用户账号</div>
						<div field="userName" sortField="USER_NAME" width="100" allowSort="true" headerAlign="center" align="left">用户姓名</div>
						<div field="orgId" sortField="ORG_ID" width="100" allowSort="true" renderer="onOpenOrgName" headerAlign="center" align="left">所属机构</div>
						<div field="telephone" width="100" headerAlign="center" align="left">电话</div>
						<div field="email" width="100" headerAlign="center" align="left">邮箱</div>
						<div field="remark" width="100"  headerAlign="center" align="left">备注</div>
						<div field="isEnable" width="100" renderer="onOpenURenderer" headerAlign="center" align="center">是否启用</div>
						<div field="isRight" width="100" renderer="onOpenURenderer" headerAlign="center" align="center">审核权</div>   
					</div>
				</div>
			</div>
			<!-- 列表展示结束 -->
		</div>
		<!-- 右部查询列表结束 -->
	</div>
</body>  
<script th:inline="javascript" type="text/javascript">
 /*<![CDATA[*/
	var orgNameList = null; 
	var grid =  null;//表格对象
	var tree = null;//树对象
	$(function(){
		  mini.parse();
		  grid = mini.get("grid1");
		  tree = mini.get("tree1");
		  queryOrg();//缓存所有组织机构
	});
	
	//缓存所有组织机构
	function queryOrg(orgId){
		 $.ajax({
			 url:basePath+"org/list",
			 type: 'POST',
		     async:false,
			 success : function(text) {
				 orgNameList = mini.decode(text);
				 tree.loadList(orgNameList, "id", "parentId");
				 grid.load();
			 },
			 error : function() {
			 }
		  });
	}
	
	var openU = [{ id: 1, text: '是' }, { id: 0, text: '否'}];
	function onOpenURenderer(e) {
	    for (var i = 0, l = openU.length; i < l; i++) {
	        var g = openU[i];
	        if (g.id == e.value) return g.text;
	    }
	    return "";
	}
	function csearch(){
		var orgNode = tree.getSelectedNode();
		var orgId = orgNode.id;//获取parentId
		grid.load({"orgId":orgId});
	}
	
	/***查询***/
	function search() {
		var  searchKey= mini.get("searchKey").getValue();
		if(searchKey!=""){
			grid.load({"searchPhrase":searchKey});
		}else{
			grid.load();
		}
	}
	
	function orgDimTreeSearch (){
		var key = mini.get("key").getValue();
	      if (key == "") {
	          tree.clearFilter();
	      } else {
	          key = key.toLowerCase();                
	          tree.filter(function (node) {
	              var text = node.orgName ? node.orgName.toLowerCase() : "";
	              if (text.indexOf(key) != -1) {
	                  return true;
	              }
	          });
	      }
	}
	
	function onOpenOrgName(e){
		var orgName="";
		var orgId = e.row.orgId;
		if(orgNameList!=null&&orgNameList.length>0){
			   for(var i=0;i<orgNameList.length;i++){
				   if(orgId==orgNameList[i].id){
					   orgName =  orgNameList[i].orgName;
					   break;
				   }  
			   }
		   }
		   return orgName;
	}
	
	/**
	添加
	**/
	function add(){
		 mini.open({
	        url:basePath+"user/edit",
	        title: "新增用户", iconCls:'icon-addnew', width: 528, height: 460,
	        onload: function () {
	            var iframe = this.getIFrameEl();
	            var selectNode = tree.getSelectedNode();//获取选中的节点
	            var orgId = "";
	            if(!!selectNode){//如果选中了节点
	            	orgId = selectNode.id;//获取当前的parentId
	            }
	            var data = { action:"new",orgId:orgId,orgNameList:orgNameList};
	            iframe.contentWindow.setData(data);
	        },
	        ondestroy: function (action) {
				if (action != "close" && action != "cancle"){
	        		grid.reload();
				}
	        }
	    });
	
	}
	
	/**
	授权角色
	**/
	function toRole(){
		var rows = grid.getSelecteds();
		if(rows.length>1){
			mini.alert("只能选择一条记录!");
			return;
		}
		var row = grid.getSelected();
	    if (row) {
			 mini.open({
		        url:basePath+"user/roleIndex",
		        title: "角色分配", iconCls:'icon-user', width: 528, height: 460,
		        onload: function () {
		             var iframe = this.getIFrameEl();
		             var userId= row.id;//用户ID
		            var data = { action:"role",userId:userId};
		            iframe.contentWindow.setData(data);
		        },
		        ondestroy: function (action) {
					if (action != "close" && action != "cancle"){
		        		grid.reload();
					}
		        }
			  });
	    }else{
			mini.alert("请选中一条记录!");
		}
	}
	/**
	编辑
	**/
	function edit(){
	
		
		var rows = grid.getSelecteds();
		if(rows.length>1){
			mini.alert("只能选择一条记录!");
			return;
		}
		var row = grid.getSelected();  
	    if (row) {
				 mini.open({
			        url:basePath+"user/edit",
			        title: "编辑用户", iconCls:'icon-edit', width: 528, height: 460,
			        onload: function () {
			             var iframe = this.getIFrameEl();
			            var data = { action:"edit",fwRightUser:row,orgNameList:orgNameList};
			            iframe.contentWindow.setData(data);
			        },
			        ondestroy: function (action) {
						if (action != "close" && action != "cancle"){
			        		grid.reload();
			        		//queryOrg(action);
						}
			        }
			    });
	    }else{
			mini.alert("请选中一条记录!");
		}
	
	}
	
	/**
	删除
	**/
	function deleteUser(){
		var rows = grid.getSelecteds();
		if (rows.length > 0 ){
	 		mini.confirm("确定删除选中记录？","提示!",function(e){
				if (e == "ok"){ 
					var rowArray = [];// 当前删除行的数组
					if (rows.length > 0) {
						for ( var int = 0; int < rows.length; int++) {
							var id = rows[int].id;
							rowArray.push(id);
							
						}
					}
				    if (rows.length > 0) {
				       if (rowArray.length != 0){
				       		var ids = rowArray.toString();
				       		 $.ajax({
				       			url:basePath+"user/delete?ids="+ids,
				       			type:"delete",
				       			data:null,
				       			success:function(successData){
				       	        	var message = mini.decode(successData);
						        	if(message.type == '200'){
						            	showMsg("用户删除成功","success",800,"center","center");
						        		grid.load();
						        	}else{
						        		showMsg("用户删除失败","warning",800,"center","center");
						        	}
						        	csearch();
				       			},
				       			error:function(errorData){
		        	 				showMsg("用户删除错误","danger",800,"center","center");
				       			}
				       		}); 
				       }
				    }
				}
			}); 
		}else{
			mini.alert("请选中一条记录!");
		}
	} 
/*]]>*/
</script>
</html>